package com.xbts.web.controller.cloud;

import com.huaweicloud.sdk.vpc.v2.model.*;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.system.domain.dto.RegionIdDTO;
import com.xbts.system.service.ICloudSecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author llk
 * @Date 2023/8/11 15:07
 */
@RestController
@RequestMapping("/cloud/security")
public class CloudSecurityController extends BaseController {

    @Autowired
    private ICloudSecurityService cloudSecurityService;

    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0001.html
     * 查询安全组列表
     * @return 安全组列表
     */
    @PostMapping("neutronListSecurityGroups")
    public AjaxResult neutronListSecurityGroups(@RequestBody RegionIdDTO<NeutronListSecurityGroupsRequest> regionIdDTO) {
        NeutronListSecurityGroupsResponse response = cloudSecurityService.neutronListSecurityGroups(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("查询安全组列表失败");
        }
        return AjaxResult.success(response);
    }

    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0002.html
     * 查询安全组
     * @return 查询安全组
     */
    @PostMapping("neutronShowSecurityGroup")
    public AjaxResult neutronShowSecurityGroup(@RequestBody RegionIdDTO<NeutronShowSecurityGroupRequest> regionIdDTO) {
        NeutronShowSecurityGroupResponse response = cloudSecurityService.neutronShowSecurityGroup(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("查询安全组失败");
        }
        return AjaxResult.success(response);
    }
    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0003.html
     * 创建安全组
     * @return 创建安全组
     */
    @PostMapping("neutronCreateSecurityGroup")
    public AjaxResult neutronCreateSecurityGroup(@RequestBody RegionIdDTO<NeutronCreateSecurityGroupRequest> regionIdDTO) {
        NeutronCreateSecurityGroupResponse response = cloudSecurityService.neutronCreateSecurityGroup(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("创建安全组失败");
        }
        return AjaxResult.success(response);
    }
    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0004.html
     * 更新安全组
     * @return 更新安全组
     */
    @PostMapping("neutronUpdateSecurityGroup")
    public AjaxResult neutronUpdateSecurityGroup(@RequestBody RegionIdDTO<NeutronUpdateSecurityGroupRequest> regionIdDTO) {
        NeutronUpdateSecurityGroupResponse response = cloudSecurityService.neutronUpdateSecurityGroup(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("更新安全组失败");
        }
        return AjaxResult.success(response);
    }
    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0005.html
     * 删除安全组
     * @return 删除安全组
     */
    @PostMapping("neutronDeleteSecurityGroup")
    public AjaxResult neutronDeleteSecurityGroup(@RequestBody RegionIdDTO<NeutronDeleteSecurityGroupRequest> regionIdDTO) {
        NeutronDeleteSecurityGroupResponse response = cloudSecurityService.neutronDeleteSecurityGroup(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("删除安全组失败");
        }
        return AjaxResult.success();
    }
    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0006.html
     * 查询安全组规则列表
     * @return 查询安全组规则列表
     */
    @PostMapping("neutronListSecurityGroupRules")
    public AjaxResult neutronListSecurityGroupRules(@RequestBody RegionIdDTO<NeutronListSecurityGroupRulesRequest> regionIdDTO) {
        NeutronListSecurityGroupRulesResponse response = cloudSecurityService.neutronListSecurityGroupRules(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("查询安全组规则列表失败");
        }
        return AjaxResult.success(response);
    }
    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0007.html
     * 查询安全组规则
     * @return 查询安全组规则
     */
    @PostMapping("neutronShowSecurityGroupRule")
    public AjaxResult neutronShowSecurityGroupRule(@RequestBody RegionIdDTO<NeutronShowSecurityGroupRuleRequest> regionIdDTO) {
        NeutronShowSecurityGroupRuleResponse response = cloudSecurityService.neutronShowSecurityGroupRule(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("查询安全组规则失败");
        }
        return AjaxResult.success(response);
    }
    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0008.html
     * 创建安全组规则
     * @return 创建安全组规则
     */
    @PostMapping("neutronCreateSecurityGroupRule")
    public AjaxResult neutronCreateSecurityGroupRule(@RequestBody RegionIdDTO<NeutronCreateSecurityGroupRuleRequest> regionIdDTO) {
        NeutronCreateSecurityGroupRuleResponse response = cloudSecurityService.neutronCreateSecurityGroupRule(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("创建安全组规则失败");
        }
        return AjaxResult.success(response);
    }

    /**
     * https://support.huaweicloud.com/api-vpc/vpc_sg02_0009.html
     * 删除安全组规则
     * @return 删除安全组规则
     */
    @PostMapping("neutronDeleteSecurityGroupRule")
    public AjaxResult neutronDeleteSecurityGroupRule(@RequestBody RegionIdDTO<NeutronDeleteSecurityGroupRuleRequest> regionIdDTO) {
        NeutronDeleteSecurityGroupRuleResponse response = cloudSecurityService.neutronDeleteSecurityGroupRule(regionIdDTO);
        if (response == null) {
            return AjaxResult.error("删除安全组规则失败");
        }
        return AjaxResult.success();
    }

}
